const http = require('http');
const fs = require('fs');

const server = http.createServer();
const readFile = function(path) {
	if (path === '/') {
		return fs.readFileSync("./demo/index.html").toString();
	}
	return fs.readFileSync("./demo/index.html").toString();
};
server.on('request', function(req, res){
	const requestUrl = req.url;

	if (req.url.endsWith('.js')) {
		console.log('捕捉到一个js文件');
		const data = fs.readFileSync('.'+requestUrl, { encoding: 'utf-8'}).toString();
		res.writeHead(200, { contentType: 'text/javascript'});
		res.end(data);
	}

	if (req.url === '/') {

	}
	// if (req.url.endsWith('js')) {

	// }
	const data = fs.readFileSync('./demo/index.html', { encoding: 'utf-8'}).toString();
	res.writeHead(200, { contentType: 'text/html'});
	res.end(data);
})

server.listen(8089);
console.log('server running at port http://localhost:8089');